package demo02;

public class MyTicket2 implements Runnable{
    int ticket = 0;
    @Override
    public void run() {
        while (true){
            if (method()) break;
        }
    }

    private synchronized boolean method() {
        synchronized (this) {
            if (ticket == 1000){
                return true;
            }else{
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                ticket++;
                System.out.println(Thread.currentThread().getName()+"售出第"+ticket+"张票");
            }
        }
        return false;
    }
}
